package cz.af.portletExams.userExamsOverview.dto;


import cz.af.portletExams.backend.ExamsOverViewBackend;
import cz.af.portletExams.model.Exam;
import cz.af.portletExams.model.ExamOverviewUser;
import cz.af.portletExams.model.Subject;
import org.joda.time.DateTime;
import org.joda.time.Days;
import org.joda.time.LocalDate;
import org.joda.time.LocalDateTime;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class DtoCreator {
    public static List<SubjectForUserExamsView> createDtos(List<Subject> subjectsForUsername, List<Exam> examsForUser, ExamOverviewUser user, ExamsOverViewBackend backend) {
        final HashMap<String, SubjectForUserExamsView> examsViewHashMap = new HashMap<String, SubjectForUserExamsView>();

        for (Subject subject : subjectsForUsername) {
            final SubjectForUserExamsView subjectForView = new SubjectForUserExamsView();
            final String subjectCode = subject.getSubjectCode();
            subjectForView.setSubjectCode(subjectCode);
            examsViewHashMap.put(subjectCode, subjectForView);
        }

        for (Exam exam : examsForUser) {
            final String subjectId = exam.getSubjectId();
            final SubjectForUserExamsView subjectForUserExamsView = examsViewHashMap.get(subjectId);

            final LocalDate subjectLocalDate = subjectForUserExamsView.getLocalDate();
            final LocalDateTime examDate = exam.getDate();
            final LocalDate examLocalDate = examDate.toLocalDate();
            if (subjectLocalDate == null || subjectLocalDate.isBefore(examLocalDate)) {
                subjectForUserExamsView.setLocalDate(examLocalDate);
                subjectForUserExamsView.setLocalTime(examDate.toLocalTime());
                final LocalDate now = LocalDate.now();
                final Integer dayDiff = Days.daysBetween(examLocalDate, now).getDays();
                subjectForUserExamsView.setDayLeft(-dayDiff);
            }
        }

        //apply custom exams
        final Map<String, String> customExams = user.getExams();
        for (Map.Entry<String, String> stringStringEntry : customExams.entrySet()) {
            final String subjectId = stringStringEntry.getKey();
            final String examId = stringStringEntry.getValue();
            final SubjectForUserExamsView subjectForUserExamsView = examsViewHashMap.get(subjectId);

            if (subjectForUserExamsView != null) {
                if (examId.equals(ExamOverviewUser.NO_EXAM)) {
                    subjectForUserExamsView.setLocalTime(null);
                    subjectForUserExamsView.setLocalDate(null);
                    subjectForUserExamsView.setDayLeft(null);
                } else {
                    final Exam exam = backend.findExam(examId);
                    final LocalDateTime examDate = exam.getDate();
                    subjectForUserExamsView.setLocalTime(examDate.toLocalTime());
                    subjectForUserExamsView.setLocalDate(examDate.toLocalDate());
                    subjectForUserExamsView.setDayLeft(-Days.daysBetween(exam.getDate().toDateTime(), DateTime.now()).getDays());
                }
            }
        }
        return new ArrayList<SubjectForUserExamsView>(examsViewHashMap.values());
    }
}
